package com.sduept.zj103.model;

public class Apci {
    public int sendNo; //控制字节
    public int recvNo; //控制字节
    public int asduLen;
    public UFrameMark uFrameMark;
    public FrameType frameType;
    public static final int START = 0x68;
    public static final int APCILEN = 7;
    public static final int CONTROLLEN = 4;

    public Apci() {
        sendNo = 0;
        recvNo = 0;
        asduLen = 0;
        uFrameMark = UFrameMark.STARTDT;
        frameType = FrameType.X;
    }

    public int getApduLen() {
        return asduLen + CONTROLLEN;
    }

    public void encodeSFrame() {
        asduLen = 0;
        frameType = FrameType.S;
        sendNo = 1;
    }

    public void encodeUFrame(UFrameMark uFrameMark) {
        asduLen = 0;
        frameType = FrameType.U;
        recvNo = 0;
        sendNo = 0;
        sendNo |= 0x03;
        if(uFrameMark == UFrameMark.STARTDT) {
            sendNo |= 0x0004;
        } else if(uFrameMark == UFrameMark.STOPDT) {
            sendNo |= 0x0010;
        } else if(uFrameMark == UFrameMark.TESTFR) {
            sendNo |= 0x0040;
        }
    }

    public void encodeUFrameResponse(UFrameMark uFrameMark) {
        asduLen = 0;
        frameType = FrameType.U;
        recvNo = 0;
        sendNo = 0;
        sendNo |= 0x03;
        if(uFrameMark == UFrameMark.STARTDT) {
            sendNo |= 0x0008;
        } else if(uFrameMark == UFrameMark.STOPDT) {
            sendNo |= 0x0020;
        } else if(uFrameMark == UFrameMark.TESTFR) {
            sendNo |= 0x0080;
        }
    }
}
